﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using SoundServant.Controls;

namespace SoundServant
{
    /// <summary>
    /// Interaction logic for LoadWindow.xaml
    /// </summary>
    partial class LoadWindow : SSWindow
    {
        public Congregation congregation;

        public LoadWindow()
        {
            InitializeComponent();

            SqlConnection connection = SS.Connection();
            VersionLabel.Content = SS.Version();
            SqlCommand command = new SqlCommand("SELECT congId, congName, congTMSSMDay, congPTWSDay FROM congregations", connection);
            SqlDataReader dbReader = command.ExecuteReader();

            while (dbReader.Read())
            {
                CongChooserControl congChooserControl = new CongChooserControl(dbReader[1].ToString(), (int)dbReader[0], (DayOfWeek)dbReader[2], (DayOfWeek)dbReader[3]);
                congChooserControl.CongClicked += new CongChooserControl.CongClickedEventHandler(congChooserControl_CongClicked);
                CongChooserStackPanel.Children.Add(congChooserControl);
            }

            dbReader.Close();
            connection.Close();
            connection.Dispose();

        }

        void congChooserControl_CongClicked(int id)
        {
            congregation = new Congregation(id);
            this.Closing -= new System.ComponentModel.CancelEventHandler(Window_Closing);
            this.Close();
        }

        public Congregation LoadCongDialog()
        {
            ShowDialog();
            return congregation;
        }


        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
        }


    }
}
